package.path      = package.path .. ";.\\LuaSocket\\?.lua"
package.cpath     = package.cpath .. ";.\\LuaSocket\\?.dll"

local myCall      = {}
local socket      = require("socket")
local socketurl   = require("url")
local ltn12       = require("ltn12")
local http        = require("http")
local socket_http = require("socket.http")

-- 发送请求的方法
local function sendRequest(url, params)
    -- 构建请求头
    local headers = {
        ["Content-Type"] = "application/x-www-form-urlencoded",
        ["Content-Length"] = tostring(#params)
    }

    -- 发送POST请求
    local response = {}
    local result, status, headers = socket.http.request {
        url = url,
        method = "POST",
        headers = headers,
        source = ltn12.source.string(params),
        sink = ltn12.sink.table(response)
    }

    -- 检查请求是否成功
    if status == 200 then
        local responseBody = table.concat(response)
        -- 处理返回的响应
        -- print("返回值: " .. responseBody)
        return responseBody
    else
        -- 请求失败，处理错误
        return "请求失败，状态码: " .. tostring(status)
    end
end


-- Hooks Start
function myCall.onPlayerConnect(id)
    -- 获取连接服务器的玩家信息
    local player = net.get_player_info(id) -- id
    local playerUcid = player.ucid         -- ucid
    local playerName = player.name         -- 用户名

    if playerName == "deathgun-watchdog" then
        return
    else
        local params = "ucid=" .. playerUcid .. "&name=" .. playerName
        local url = "http://localhost:8079/api/player/login"

        local response = sendRequest(url, params)
        net.send_chat_to(id, response)
    end
end


-- function myCall.onPlayerTryChangeSlot(playerID, side, slotID)
--     return true
-- end

function myCall.onPlayerTrySendChat(playerID, msg, all) -- 截获用户输入
    if msg == "-myinfo" or msg == "-MYINFO" then
        local player = net.get_player_info(playerID)
        local playerUcid = player.ucid
        local url = "http://localhost:8079/api/player/myInfo"
        local params = "ucid=" .. tostring(playerUcid)
        local response = sendRequest(url, params)
        net.send_chat_to(response, playerID)
        return ""
    else
        return msg
    end
    
    
end

-- onGameEvent(eventName,arg1,arg2,arg3,arg4,arg5,arg6,arg7)
-- --"friendly_fire", playerID, weaponName, victimPlayerID
-- --"mission_end", winner, msg
-- --"kill", killerPlayerID, killerUnitType, killerSide, victimPlayerID, victimUnitType, victimSide, weaponName
-- --"self_kill", playerID
-- --"change_slot", playerID, slotID, prevSide
-- --"connect", playerID, name
-- --"disconnect", playerID, name, playerSide, reason_code
-- --"crash", playerID, unit_missionID
-- --"eject", playerID, unit_missionID
-- --"takeoff", playerID, unit_missionID, airdromeName
-- --"landing", playerID, unit_missionID, airdromeName
-- --"pilot_death", playerID, unit_missionID

function myCall.onGameEvent(eventName, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
    if net.get_player_info(arg1) then
        local player = net.get_player_info(arg1) -- get player info
        local playerUcid = player.ucid
        if eventName == "kill" and arg3 == arg6 then
            local params = "ucid=" .. playerUcid
            local url = "http://localhost:8079/api/player/teamkill"
            net.send_chat(player.name .. "伤害了友军! 扣除分数200", true)
            net.send_chat_to(sendRequest(url, params), arg1)
        -- elseif eventName == "kill" then
        --     local params = "ucid=" .. playerUcid .. "&typeName=" .. tostring(arg5) -- 获得击杀
        --     local url = "http://localhost:8079/api/player/kill"
        --     net.send_chat_to(sendRequest(url, params), arg1)
        --     -- net.send_chat("获得了击杀,TypeName: " .. arg5)
        elseif eventName == "landing" then -- 降落
            local params = "ucid=" .. playerUcid
            local url = "http://localhost:8079/api/player/landing"
            net.send_chat_to(sendRequest(url, params), arg1)

        elseif eventName == "crash" then                                            -- 死亡
            local params = "ucid=" .. playerUcid
            local url = "http://localhost:8079/api/player/dead"
            net.send_chat_to(sendRequest(url, params), arg1)
        end
    end
end

DCS.setUserCallbacks(myCall)
